import array
import operator
import sys
import ConfigParser

def lookup_order(config, featureId):
    if config.has_section(featureId):
        items = config.items(featureId)
        returned_values = []
        for item in sorted(items, key=operator.itemgetter(1)):
            returned_values.append(item[0])
        return returned_values

    if featureId == "defaults":
        return []

    return lookup_order(config, "defaults")

if __name__ == "__main__":
    if len(sys.argv) > 2:
        fn = sys.argv[1]
        featureId = sys.argv[2]

        if not featureId is None:
            config = ConfigParser.RawConfigParser()
            config.optionxform = str
            config.read(fn)

            results = [x for (x) in lookup_order(config, featureId)]
            print results
            exit(0)

    print "Usage: lookup_ordered_feature_values.py FILENAME FEATURE_ID"
    exit(-1)
